'use strict';
/**
 * Created by FreezeSoul on 2015/10/24.
 */
define([
    "app/core/base",
    "app/core/util",
    "../commonMeta",
    "jquery",
    "underscore",
    'JSONParse',
    'loglevel',
    "audiojs",
    'jqueryTmpl',
    'jQueryUUID'
], function (Chart, Util, Meta, $, _, JSON, log) {

    var property = {
        config: "property",
        option: [
            {
                group: '基础',
                id: 'YinPinZiYuanLuJing',
                name: '音频资源路径',
                enable: true,
                type: 'resource',
                value: '',
                default: '',
                link: 'audioUrl',
                tooltip: '设置音频的资源路径，格式要求MP3，外部视频http作为前缀'
            },
            {
                group: '基础',
                id: 'YinPinZiDongBoFang',
                name: '自动播放',
                enable: false,
                type: 'checked',
                value: true,
                default: true,
                link: 'autoPlay',
                tooltip: '设置音频自动播放'
            },
            {
                group: '基础',
                id: 'YinPinShowControl',
                name: '显示控制按钮',
                enable: false,
                type: 'checked',
                value: true,
                default: true,
                link: 'controls',
                tooltip: '设置音频显示控制按钮'
            },
            {
                group: '基础',
                id: 'YinPinLoop',
                name: '是否循环',
                enable: false,
                type: 'checked',
                value: true,
                default: true,
                link: 'loop',
                tooltip: '设置音频是否循环'
            }
        ].concat($.extend(true, [], Meta.commonProperties)),
        MapOption: function (option) {
            Util.linkOption(this.option, option);
        }
    };

    var image = Chart.extend({
        constructor: function (layout) {
            this._layout = layout;
            this._element = null;
            this._container = null;
            this._audioPlayer = null;
            this._uuid = "audio_" + $.uuid();
            this._template = '<audio src="${audioUrl}" id="${audioId}" preload="auto" ${loop}></audio>';
            this._option = {
                Series: [],
                Events: [],
                Extend: $.extend(true, {}, property),
                Option: {
                    uuid: '',
                    class: '',
                    display: true,
                    audioUrl: '',
                    autoPlay: true,
                    controls: true,
                    loop: true,
                    initScript: '',
                    disposeScript: ''
                }
            };
        },
        init: function (element) {
            try {
                this._container = element;
            } catch (e) {
                log.error(e);
            }
        },
        _readProperty: function () {
            try {
                if (!this._option.Extend) return;
                Util.deepMapOption(property, this._option.Extend, this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        _render: function (option) {
            var _this = this;
            var audioUrl = option.audioUrl;
            this._uuid = "audio_" + $.uuid();
            if (option.audioUrl.toLowerCase().indexOf("http") === 0) {
                audioUrl = option.audioUrl;
            } else {
                var rootPath = "static/resource/";
                audioUrl = adConfig.baseServiceUrl + rootPath + option.audioUrl;
            }
            var $element = $.tmpl(this._template, {
                audioId: _this._uuid,
                audioUrl: audioUrl,
                loop: option.loop ? "loop" : ""
            });
            if (this._audioPlayer) {
                this._audioPlayer.pause();
            }
            $(this._container).empty();
            $element.appendTo(this._container);
            if (option.controls && option.audioUrl) {
                window.audiojs.events.ready(function () {
                    _this._audioPlayer = window.audiojs.create($element[0]);
                    if (option.autoPlay) {
                        _this._audioPlayer.play();
                    }
                });
            } else {
                this._audioPlayer = $element[0];
                if (option.autoPlay) {
                    this._audioPlayer.play();
                }
            }
            //Util.setCommonMeta(this, this._container, this._option.Option);
            //Util.initChartLifeTime(this, this._container, this._option.Option);
            return $element;
        },
        example: function () {
            try {
                this._element = this._render(this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        _bindData: function (data) {
        },
        setData: function (data) {
            try {
                this._readProperty();

                if (data
                    && this._option.Binding
                    && this._option.Binding.length > 0
                    && this._option.Extend
                ) {
                    this._bindData(data);
                }
                this._element = this._render(this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        setOption: function (option) {
            try {
                return this._option = $.extend(true, {}, option);
            } catch (e) {
                log.error(e);
            }
        },
        getOption: function () {
            try {
                this._readProperty();
            } catch (e) {
                log.error(e);
            }
            return this._option;
        },
        setTheme: function (theme) {
            try {
            } catch (e) {
                log.error(e);
            }
        },
        resize: function () {
            try {
            } catch (e) {
                log.error(e);
            }
        },
        showLoading: function () {
            try {
                if (this._layout && this._layout.showLoading) {
                    this._layout.showLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        hideLoading: function () {
            try {
                if (this._layout && this._layout.hideLoading) {
                    this._layout.hideLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        dispose: function () {
            try {
                if (this._audioPlayer) {
                    this._audioPlayer.pause();
                }
                $(this._element).off().remove();
                //Util.disposeChartLifeTime(this, this._container, this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        getElement: function () {
            return this._element;
        },
        getChart: function () {
            return this._element;
        }
    });

    return {
        Chart: image,
        Property: property
    };
});